package com.yc.goods.controller;

import com.yc.entity.Goodsinfo;
import com.yc.goods.biz.GoodsInfoBiz;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("goods")
@Slf4j
public class GoodsInfoController {
    @Autowired
    private GoodsInfoBiz goodsInfoBiz;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("findTop")
    public Map<String, Object> findTop() {
        String key = "goodsInfo:";

        Map<String, Object> map = this.goodsInfoBiz.findbyTnameTop();
        return map;
    }

    @GetMapping("findByGname")
    public Map<String, Object> findByGname(String gname) {
        Map<String, Object> map = new HashMap<>();
        List<Goodsinfo> goodsinfos = goodsInfoBiz.findByGname(gname);
        List<Goodsinfo> updatedGoodsinfos = new ArrayList<>();
        for (Goodsinfo info : goodsinfos) {
            String[] pics = info.getPics().split(";");
            String pic = pics[0];
            info.setPics(pic);
            updatedGoodsinfos.add(info);
        }
        // 在循环之后更新goodsinfos列表
        goodsinfos.clear();
        goodsinfos.addAll(updatedGoodsinfos);
        ArrayList<Object> searchGoodsNames = new ArrayList<>();
        for (Goodsinfo g:goodsinfos){
            searchGoodsNames.add(g.getGname());
        }
        map.put("searchGoodsNames",searchGoodsNames);
        map.put("goods",goodsinfos);
        return map;
    }

    @GetMapping("findByGno")
    public Goodsinfo findByGno(Integer gno){
        System.out.println(gno);
        Goodsinfo info = this.goodsInfoBiz.findByGno(gno);
        return info;
    }

}
